package com.guit.hrms.controller;

import com.guit.hrms.po.department.Department;
import com.guit.hrms.po.utilsPO.PageBean;
import com.guit.hrms.po.utilsPO.Result;
import com.guit.hrms.service.DepartmentService;
import com.guit.hrms.vo.department.DepartmentVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/api/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 新增部门
     * @param department 部门信息
     */
    @PostMapping
    public Result addDepartment(@RequestBody Department department){
        System.out.println("新增的部门---------------------------");
        System.out.println(department);
        Integer result = departmentService.addDepartment(department);
        if(result==0){
            return Result.error("添加失败,部门名称已存在");
        } else if (result == 2) {
            return Result.error("请完整输入要添加的内容");
        } else{
            return Result.success("添加成功");
        }
    }

    /**
     * 更新部门信息
     * @param department 要更新的部门信息
     */
    @PatchMapping
    public Result updateDepartment(@RequestBody Department department){
        Integer result = departmentService.updateDepartment(department);
        if(result == 0){
            return Result.error("更新失败,该部门不存在");
        } else if (result == 2) {
            return Result.error("请输入要修改的内容");
        } else if (result == 3) {
            return Result.error("请输入要修改信息部门的id");
        } else{
            return Result.success("更新成功");
        }
    }

    /**
     * 查询部门
     * @param deptId 要查询的部门id
     */
    @GetMapping
    public Result queryDepartment(Integer deptId){
        if (deptId != null){
            DepartmentVO department = departmentService.selectDepartment(deptId);
            if (department == null){
                return Result.error("该部门不存在");
            } else {
                return Result.success(department);
            }
        } else {
            List<DepartmentVO> departments = departmentService.selectDepartments();
            return Result.success(departments);
        }

    }

    /**
     * 删除部门
     * @param deptIds 要删除的部门id
     */
    @DeleteMapping("/{deptIds}")
    public Result deleteDepartment(@PathVariable List<Integer> deptIds){
        System.out.println(deptIds.toString());
        if (deptIds.isEmpty()){
            return Result.error("请输入要删除部门的id");
        } else {
            Integer result = departmentService.deleteDepartment(deptIds);
            if (result == 0){
                return Result.error("删除失败,请确定部门id是否正确");
            } else {
                return Result.success("删除成功");
            }
        }
    }

    /**
     * 查询部门下各个岗位的员工
     * @param page
     * @param pageSize
     * @param deptId
     * @param positionId
     * @return
     */
    @GetMapping("/person")
    public Result getDeptPerson(@RequestParam(defaultValue = "1")Integer page,
                                @RequestParam(defaultValue = "10")Integer pageSize,
                                Integer deptId,Integer deptType,Integer positionId){

        PageBean pageBean = departmentService.getDeptPerson(page,pageSize,deptId,deptType,positionId);

        return Result.success(pageBean);
    }

}
